﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Douban.Core.HotKeys;
using Douban.Core.Interop;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace Douban
{
    /// <summary>
    /// Interaction logic for SettingWindow.xaml
    /// </summary>
    public partial class SettingWindow : ISettingView
    {
        private IList<ModifierKeys> _modifierKeys;
        private KeyHook _keyHook;

        public SettingWindow()
        {
            InitializeComponent();
            this.Closing += OnClosing;
            this.KeyDown += OnKeyDown;
            _keyHook = new KeyHook(KeyHandler);
        }

        private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _keyHook.Unload();
        }

        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.None && e.Key == Key.Escape)
                CloseView();
        }

        public ISettingViewModel Context
        {
            get { return DataContext as ISettingViewModel; }
            set { DataContext = value; }
        }

        public void CloseView()
        {
            this.Close();
            SettingWindowHelper.IsOpen = false;
        }

        public void ShowView()
        {
            this.Show();
            this.Activate();
        }

        public void ShowSuccessMessage()
        {
            MessageBox.Show("保存成功");
        }
         
        private bool KeyHandler(KeyboardHookStruct keyboardHookStruct)
        {
            Key virtualKey = KeyInterop.KeyFromVirtualKey(keyboardHookStruct.vkCode);
            return Keyboard.Modifiers == ModifierKeys.Control &&
                (virtualKey == Key.V || virtualKey == Key.C || virtualKey == Key.X || virtualKey == Key.A);
        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
            TextBox hotKeyInputBox = (TextBox)sender;

            if(_modifierKeys == null) _modifierKeys = new List<ModifierKeys>(2);
            _modifierKeys.Clear();

            if (Keyboard.Modifiers == ModifierKeys.None)
            {
                _modifierKeys.Add(ModifierKeys.Control);
                _modifierKeys.Add(ModifierKeys.Alt);
            }

            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                _modifierKeys.Add(ModifierKeys.Control);

            if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt
                && Keyboard.Modifiers != ModifierKeys.Alt)
            {
                _modifierKeys.Add(ModifierKeys.Alt);
            }

            string hotKeyText = "";
            foreach(ModifierKeys key in _modifierKeys)
            {
                if(key != ModifierKeys.None) hotKeyText += (key.ToString() + " + ");
            }
            
            if(e.Key >= Key.A && e.Key <= Key.Z) hotKeyText += e.Key.ToString();

            hotKeyInputBox.Text = hotKeyText;
        }
    }
}
